/**
 * @author Vladimir Kanubrikov
 */
function removeItem(elem){		
		var deletedEl = $(elem).parent().parent();
		constructorField.removeItem(deletedEl);
};

$(function(){	
	$('.mr-option').draggable({
        helper : 'clone',
        opacity : 0.3
	});

/**
 * Constructor field object
 */
	 constructorField={};
		constructorField.removeItem = function(deletedEl){
			if(deletedEl.length!=0){
		 		deletedEl.uid = deletedEl.attr('id').match(/\d+/);
				constructorField.prepareToRegExp.splice(deletedEl.uid,1);
				constructorField.prepareToRegExp.counter--;
				deletedEl.remove();
			}
		};
		constructorField.templates={
				custom:$("#csCustom"),
				native:$("#cs")
		};
		constructorField.mainContainer = $('#mrContainer');
		constructorField.managingElement = $('#csCustomText');
		constructorField.managingElementWrapper = $('#csCTWrap');
		constructorField.mainContainer.droppable({
	        accept : 'div.mr-option',
	        drop : function(event, ui) {
	        	//console.log(testT =ui.draggable);
	        	switch ($(ui.draggable).attr('data-type')){
	        	case 'char':	new CharRegexpElem(ui.draggable).initCS();
	        	break;
	        	default: new SimpleRegexpElem(ui.draggable).initCS();
	        	}
	           	
	           	constructorField.managingElement.focus();	           	
	        }
		});	
		var i = 0;
		constructorField.managingElement.keyup(function(e){
			if(e.keyCode == 8){
				console.log($(this).val().length, i);
				
				if($(this).val().length==0){
					(i>0)?(function(){var deletedEl = constructorField.mainContainer.children('span').last();
					constructorField.removeItem(deletedEl);
					i = 0;})():i++;
				}
			}
			else if(e.keyCode == 13){
				ctElement = $(document.createElement('span')).attr('id', 'el'+constructorField.prepareToRegExp.counter).loadTemplate(constructorField.templates.custom, {customTxt: $(this).val()});
				constructorField.managingElementWrapper.before(ctElement);			
				ctElement.uid=constructorField.prepareToRegExp.counter;
				ctElement.name = $(this).val();
				constructorField.prepareToRegExp.push(ctElement);
				constructorField.prepareToRegExp.counter++;
				$(this).val("");
			}
		});
		constructorField.prepareToRegExp = [];
		constructorField.prepareToRegExp.counter = 0;
	
/**
 * Regexp Element Class // do not use in itself.
 */		
		var SimpleRegexpElem = function(elem){
			this.name = elem.context.textContent;
		};				
		SimpleRegexpElem.prototype.settings = { quantification:''	};
		SimpleRegexpElem.prototype.view = {confTextField: constructorField.managingElementWrapper};	
		SimpleRegexpElem.prototype.initCS = function(){	
			this.uid = constructorField.prepareToRegExp.counter;
			this.view.confTextField.before($(document.createElement('span')).attr('id','el'+this.uid).loadTemplate(constructorField.templates.native, {elemName: this.name}));	
			constructorField.prepareToRegExp.push(this);
			constructorField.prepareToRegExp.counter++;
			this.settingsButtonBind();
		} ;	
		SimpleRegexpElem.prototype.settingsButtonBind = function(){
			var self = this;
			setTimeout(function(){
				$('#el'+self.uid).click(function(){
					
				});
			},0);
			
			
		};
	
/**
 * Childs of Regexp Element Class which could be using.
 */
	CharRegexpElem = function(elem){
		this.name = elem.context.textContent;
		this.settings = [['onlyChars', /\w/],['noSpaces', /\S/], ['noDigits', /\D/]];
		this.configuration = {};
	};
	CharRegexpElem.prototype = Object.create(SimpleRegexpElem.prototype);
	
	
});


